package net.maku.newbase;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页 入参
 *
 * @author JYL
 * @date 2022/6/18 15:46
 */
@Data
public class PageParam {

    private Integer current = 1;
    private Integer size = 10;

    private Integer page=1;
    private Integer limit=10;
    /**
     * 入参格式[{},{}]jsonArray;
     * <hr>
     * 例子:[{"column":"create_time","asc":true}]
     */
    private String orders;

    public PageParam() {

    }

    public Integer getCurrent() {
        return page;
    }

    public Integer getSize() {
        return limit;
    }

    public PageParam(Integer size) {
        this.size = size;
    }

    public List<OrderItem> generateOrderList() {
        List<OrderItem> orderItemList = new ArrayList<>();
        try {
            orderItemList = JSONArray.parseArray(orders, OrderItem.class);
        } catch (Exception e) {

        }
        return orderItemList;
    }

    public <K> IPage<K> buildPage() {
        Page<K> page = new Page<>(getCurrent(), getSize());
        if (StringUtils.isNotBlank(orders)) {
            page.addOrder(generateOrderList());
        }
        return page;
    }

    /**
     * 构建 分页对象 (糖块)
     *
     * @param <K>
     * @return
     */
    public <K> IPage<K> b() {
        return buildPage();
    }

}
